/**
 * 
 */
package com.jm.web.builder;

import java.io.IOException;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.data.ReturnData;

/**
 * @author yanjie
 *
 * 2011/9/22 上午9:38:57
 * 
 */
public class ErrorMessageBuilder extends BaseBuilder{
	@Override
	public void RenderEnd() {
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("<table width=\"99%%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" ><tr><td><div id=\"%1$s\" class=\"errorMsg\" style=\"display:none;height:auto;margin-top:1px;margin-left:2px;width:100%%\"></div></td></tr></table>"
				,this.getBaseTag().GetExtValue("id","errorMsg"));
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//處理錯誤的消息
		ReturnData ret = this.getBaseTag().getPage().getReturnData();
		if(!this.getBaseTag().IsContainsKey("id") && ret != null && (ret.isError() 
				|| !StringHelper.IsNullOrEmpty(ret.getCallResult().getOkInfo())
				|| !StringHelper.IsNullOrEmpty(ret.getCallResult().getWarningInfo())
				|| !StringHelper.IsNullOrEmpty(ret.getCallResult().getPromptInfo())
				|| !StringHelper.IsNullOrEmpty(ret.getCallResult().getOkInfo())
				|| !StringHelper.IsNullOrEmpty(ret.getCallResult().getJSBeforeCode())
				|| !StringHelper.IsNullOrEmpty(ret.getCallResult().getJSCode())
				)){
			sb.Reset();
			sb.Append("$P.processresult(%1$s);$P.refresh();",ret.getCallResult().ToJSONString());
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, sb.toString());
		}
	}
}
